<template>
	<om-formsub-outer :title="title" :index="index" @save="save" :deleteBtn="false">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {subOnLoad, formValidation, changeEditor, configSelectData} from '../../../static/js/formUtil.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return {
				formdata:{},
				initValue:{
				},
				autoformdata: [],
				cachename:'',
				index:null,
				datas:[],
				order:0,
				title:""
			}
		},
		methods: {
			save(){
				let _that = this
				var checkRes = formValidation(this.$refs.autoform.configData, this.formdata)
				if (!checkRes) {
					if(this.index != null){
						Object.assign(this.datas[this.index],this.formdata)
					}else{
						this.datas.push(this.formdata)
					}
					uni.setStorage({
						key: this.cachename,
						data: this.datas,
					})
					uni.navigateBack()
				} else {
					_that.om.toast(checkRes);
				}
			},
			del() {
				if(this.index != null) {
					this.datas.splice(this.index,1)
				}
				uni.setStorage({
					 key: this.cachename,
					 data: this.datas
				})
				uni.navigateBack()
			},
		},
		onLoad(e){
			let _that = this
			subOnLoad(_that, e, function() {
				_that.title = e.title
				if(e.configDataCacheKey){
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if(res.data && res.data instanceof Array){
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								for(let item of res.data){
									if(e.title){
										item.title = e.title
									}
								}
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								_that.$nextTick(function(){
									_that.$refs.autoform.initSelectIndex(_that.autoformdata[0].key,"flag")
								})
							}
						}
					});
				}
			})
		}
	}
</script>

<style>

</style>
